//
//  YXWelcomeViewController.swift
//  yanXiangWeiBo
//
//  Created by 颜祥 on 15/12/17.
//  Copyright © 2015年 yanxiang. All rights reserved.
//

import UIKit
import SDWebImage

class YXWelcomeViewController: UIViewController
{
    // MARK: - 设置全局头像底部的约束
    private var iconViewBottomConstriant: NSLayoutConstraint?

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // 添加子控件
        prepareUI()
        
        // 先显示上一次保存的头像
        setIconView()
        
        // 加载最新的用户头像
        YXUserAccount.loadUserAccount()?.loadUserInfo({ (error) -> () in
            
            if error != nil
            {
                print("获取用户信息有误")
                return
            }
            
            // 获取用户信息没有错,加载最新用户头像
            self.setIconView()
            
        })
     }
    
    // 显示当前用户的头像
    private func setIconView()
    {
        // 获取当前用户头像的地址
        if let avatar_large = YXUserAccount.loadUserAccount()?.avatar_large
        {
            let url = NSURL(string: avatar_large)!
            print("url: \(url)")
            
            // 显示当前用户头像
            iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
    }
    
    // MARK: - 欢迎界面已经出现的时候，开始动画
    override func viewDidAppear(animated: Bool)
    {
        super.viewDidAppear(animated)
        
        // 头像
        self.iconViewBottomConstriant?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        
        // 动画
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                // 文字渐变显示
                self.welcomeLabel.alpha = 0
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    self.welcomeLabel.hidden = false
                    
                    }, completion: { (_) -> Void in
                        print("欢迎界面动画完成")
                        
                        // 切换控制器，进入到主控制器
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
                        
                })
                
                
        }
        
        
    }
    
    // MARK: - 添加子控件
    private func prepareUI()
    {
        
        // 添加子控件
        view.addSubview(backView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 添加约束
        backView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
            
        // 背景图片
       view.addConstraint( NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 头像
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        iconViewBottomConstriant = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        view.addConstraint(iconViewBottomConstriant!)
        
        // 欢迎归来Label
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    
    
    }

  
    // MARK: - 懒加载
    // 背景图片
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 头像
    lazy var iconView: UIImageView =
    {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 图片变为圆形
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true

        
        return imageView
    }()
    
    // 欢迎归来Label
    lazy var welcomeLabel: UILabel =
    {
    
        let label = UILabel()
        
        // 设置文字内容
        label.text = "欢迎归来"
        
        // 设置字体大小
        label.font = UIFont.systemFontOfSize(18)
        
        label.textColor = UIColor.orangeColor()
        
        // 隐藏
        label.hidden = true
        
        
        
        return label
    
    }()
    

}
